package com.cn.wanxi.servlet.products;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.IProductsService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.service.impl.ProductsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/producat/findByIds")
public class ProductFindByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        //2.接收前台传数据过来
        String id=req.getParameter("id");
        int productid=id==null ? 0 : Integer.parseInt(id);//一个值，不封装

        IProductsService iProductsService =new ProductsServiceImpl();

        ProductModel productModel=iProductsService.findbyId(productid);
        req.getSession().setAttribute("imgHref",productModel.getImgHref());
        //7.将数据转发到页面
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("productModel",productModel);
        resp.getWriter().println(jsonObject);
    }
}
